﻿@namespace BootstrapBlazor.Components
@typeparam TItem

<div class="float-start table-toolbar-button">
    <CascadingValue Value="this" IsFixed="true">
        @ChildContent
    </CascadingValue>
    <RenderTemplate>
        @if (Buttons.Count > 0)
        {
            <div class="@ToolbarClassString">
                @foreach (var button in Buttons)
                {
                    @if (button is TableToolbarButton<TItem> b && b.IsShow)
                    {
                        <Button AdditionalAttributes="b.AdditionalAttributes" Size="b.Size"
                                Color="@b.Color" Icon="@b.Icon" Text="@b.Text" IsAsync="@b.IsAsync"
                                OnClickWithoutRender="() => OnToolbarButtonClick(b)"
                                TooltipText="@b.TooltipText" TooltipPlacement="@b.TooltipPlacement" TooltipTrigger="@b.TooltipTrigger"
                                IsDisabled="GetDisabled(b)"></Button>
                    }
                    else if (button is TableToolbarPopconfirmButton<TItem> pb && pb.IsShow)
                    {
                        <PopConfirmButton AdditionalAttributes="pb.AdditionalAttributes" CustomClass="@pb.CustomClass"
                                          Color="@pb.Color" Icon="@pb.Icon" Text="@pb.Text" Size="pb.Size" ShowShadow="@pb.ShowShadow"
                                          IsAsync="@pb.IsAsync" IsDisabled="@pb.IsDisabled" IsBlock="@pb.IsBlock" IsOutline="@pb.IsOutline"
                                          OnBeforeClick="@pb.OnBeforeClick" OnClose="@pb.OnClose" OnConfirm="() => OnToolbarConfirmButtonClick(pb)"
                                          ConfirmIcon="@pb.ConfirmIcon" ConfirmButtonColor="@pb.ConfirmButtonColor"
                                          ConfirmButtonText="@pb.ConfirmButtonText" CloseButtonColor="@pb.CloseButtonColor"
                                          Placement="@pb.Placement" Trigger="@pb.Trigger"
                                          CloseButtonText="@pb.CloseButtonText" Content="@pb.Content">
                        </PopConfirmButton>
                    }
                }
            </div>
            @if (IsAutoCollapsedToolbarButton)
            {
                <div class="btn-gear btn-group d-sm-none">
                    <button class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" type="button">
                        <i class="fa-fw fa-solid fa-gear"></i>
                    </button>
                    <div class="dropdown-menu">
                        @foreach (var button in Buttons)
                        {
                            @if (button is TableToolbarButton<TItem> b && b.IsShow)
                            {
                                <div class="dropdown-item" @onclick="e => OnToolbarButtonClick(b)">
                                    @if (string.IsNullOrEmpty(b.Icon))
                                    {
                                        <span>@b.Text</span>
                                    }
                                    else
                                    {
                                        <i class="@b.Icon"></i>
                                    }
                                </div>
                            }
                            else if (button is TableToolbarPopconfirmButton<TItem> pb && pb.IsShow)
                            {
                                <div class="dropdown-item" @onclick="pb.OnConfirm">
                                    @if (string.IsNullOrEmpty(pb.Icon))
                                    {
                                        <span>@pb.Text</span>
                                    }
                                    else
                                    {
                                        <i class="@pb.Icon"></i>
                                    }
                                </div>
                            }
                        }
                    </div>
                </div>
            }
        }
    </RenderTemplate>
</div>
